// 方法装饰器

// 不带参数的方法拦截器
// targetClassPrototype 类的原型对象(RoleService.prototype)
// methodname 方法名
// methodDecriptor  数据属性 { value: null, writable: true, enumerable: true, configurable: true }
function MyMethodDecorator(targetClassPrototype: any, methodname: string, methodDecriptor: PropertyDescriptor) {
  console.log("targetClassPrototype: ", targetClassPrototype)
  console.log("methodname: ", methodname)
  console.log("methodDecriptor: ", methodDecriptor)
  methodDecriptor.value()  // 分配角色...
}

// 不带参数的方法拦截器
function MyMethodDecorator2(methodPath: any) {
  return function MethodDecorator(targetClassPrototype: any, methodname: string, methodDecriptor: PropertyDescriptor) {
    console.log("targetClassPrototype: ", targetClassPrototype)
    console.log("methodname: ", methodname)
    console.log("methodPath: ", methodPath)
    console.log("methodDecriptor: ", methodDecriptor)
    methodDecriptor.value()  // 分配角色...
  }
}

// 方法装饰器的拦截器
function MyMethodDecorator3(targetClassPrototype: any, methodname: string, methodDecriptor: PropertyDescriptor) {
  let method = methodDecriptor.value
  methodDecriptor.value = function(...args: any) {
    console.log("拦截器前 JS逻辑")
    method.call(this, ...args)
    console.log("拦截器后 JS逻辑")
  }
}

// 目标类
class RoleService {
  public roleName: string = "管理员"
  constructor() {}

  // @MyMethodDecorator
  // @MyMethodDecorator2("/search")
  @MyMethodDecorator3
  DistribRoles() {  // 分配角色
    console.log("分配角色...", this.roleName)
  }
}
let role = new RoleService()
role.DistribRoles()
export{}